struct ListNode
{
    int val;
    ListNode *next;
    ListNode() : val(0), next(nullptr) {}
    ListNode(int x) : val(x), next(nullptr) {}
    ListNode(int x, ListNode *next) : val(x), next(next) {}
};

#include <iostream>
#include <unordered_map>
#include <queue>
using namespace std;
class Solution
{
public:
    struct Greater
    {
        bool operator()(const ListNode *list1, const ListNode *list2)
        {
            return list1->val > list2->val;
        }
    };
    ListNode *mergeKLists(vector<ListNode *> &lists)
    {
        if (lists.empty())
            return nullptr;
        ListNode *head = new ListNode;
        ListNode *cur = head;
        priority_queue<ListNode *, vector<ListNode *>, Greater> q;
        for (auto e : lists)
            if (e)
                q.push(e);
        while (q.size())
        {
            ListNode *min = q.top();
            q.pop();
            cur->next = min;
            cur = cur->next;
            min = min->next;
            if (min)
                q.push(min);
        }
        cur = head;
        head = head->next;
        delete cur;
        return head;
    }
};